package bigdata.backend.mata.test;

import bigdata.backend.datasource.entity.Datasource;
import bigdata.backend.datasource.service.DatasourceService;
import bigdata.backend.meta.dto.DorisTableWithColumns;
import bigdata.backend.meta.entity.DorisColumnMeta;
import bigdata.backend.meta.entity.DorisTableMeta;
import bigdata.backend.meta.mapper.DorisTableMetaMapper;
import bigdata.backend.meta.service.DorisMetaService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.Map;

@Slf4j
@SpringBootTest
public class TestDorisTableMeta {

    @Autowired
    private DorisTableMetaMapper dorisTableMetaMapper;

    @Autowired
    private DorisMetaService dorisMetaService;

    @Autowired
    private DatasourceService datasourceService;

    @Test
    public void test01() {
        List<DorisTableMeta> list = dorisTableMetaMapper.selectList(null);
        list.forEach(System.out::println);
    }

    @Test
    public void test02() {
        // 1. 获取数据源（确保存在且可连接）
        Long datasourceId = 2001L;
        String tableName = "info";
        Datasource ds = datasourceService.getById(datasourceId);

        try {
            // 2. 获取完整元数据（主表 + 所有列）
            DorisTableWithColumns fullMeta = dorisMetaService.getDorisTableAndColumnMeta(datasourceId, tableName);

            // 3. 获取主表元数据
            DorisTableMeta tableMeta = fullMeta.getTableMeta();

            // 4. 打印主表信息
            log.info("=== Doris 表元数据 ===");
            log.info("表名: {}", tableMeta.getTableName());
            log.info("表类型 (KEY 模型): {}", tableMeta.getTableType());
            log.info("KEY 列: {}", tableMeta.getKeyColumns());
            log.info("分区信息: {}", tableMeta.getPartitionInfo());
            log.info("分桶列: {}", tableMeta.getBucketColumns());
            log.info("分桶数: {}", tableMeta.getBucketNum());
            log.info("副本数: {}", tableMeta.getReplicationNum());
            log.info("PROPERTIES: {}", tableMeta.getProperties());

            // 5. 打印每个字段的详细信息
            log.info("\n=== 字段元数据列表 (共 {} 个字段) ===", fullMeta.getColumns().size());
            for (DorisColumnMeta col : fullMeta.getColumns()) {
                log.info(
                        "字段: {} | 类型: {} | 可空: {} | 默认值: '{}' | 注释: '{}' | " +
                                "ORDINAL: {} | 是KEY: {} | 是分区字段: {}",
                        col.getColumnName(),
                        col.getColumnType(),
                        col.getNullable(),
                        col.getDefaultValue() != null ? col.getDefaultValue() : "NULL",
                        col.getColumnComment(),
                        col.getOrdinalPosition(),
                        col.getIsKey(),
                        col.getIsPartitionColumn()
                );
            }

            // 6. 【可选】保存到元数据库
             dorisMetaService.saveDorisTableMeta(fullMeta);
             log.info("元数据已保存，主表ID: {}", tableMeta.getId());

        } catch (Exception e) {
            log.error("获取 Doris 表元数据失败: {}.{}", ds.getDefaultDatabase(), tableName, e);
        }
    }

    @Test
    public void test03() {
        Datasource ds = datasourceService.getById("2001");
        List<Map<String, Object>> list = datasourceService.getTableColumns(ds, "test_mysql2doris");
        for (Map<String, Object> map : list) {
            System.out.println(map);
        }
    }
}
